/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.dao.hibernate;

import java.util.HashMap;
import java.util.Map;

import org.openinsula.vulcano.orm.dao.query.projection.CountProjection;
import org.openinsula.vulcano.orm.dao.query.projection.MaxProjection;
import org.openinsula.vulcano.orm.dao.query.projection.MinProjection;
import org.openinsula.vulcano.orm.dao.query.projection.ProjectionVisitor;
import org.openinsula.vulcano.orm.dao.query.projection.SinglePropertyProjection;
import org.openinsula.vulcano.orm.dao.query.projection.SumProjection;

/**
 * @author Eduardo Rebola
 * 
 */
public class HqlProjectionVisitor implements ProjectionVisitor<String> {
	private Map<Class<? extends SinglePropertyProjection>, String> propertyProjectionMapping;

	public HqlProjectionVisitor() {
		initializeMappings();
	}

	private void initializeMappings() {
		propertyProjectionMapping = new HashMap<Class<? extends SinglePropertyProjection>, String>();
		propertyProjectionMapping.put(CountProjection.class, "count");
		propertyProjectionMapping.put(MaxProjection.class, "max");
		propertyProjectionMapping.put(MinProjection.class, "min");
		propertyProjectionMapping.put(SumProjection.class, "sum");
	}

	public String visit(final CountProjection projection) {
		return propertyProjectionVisit(projection);
	}

	public String visit(final MaxProjection projection) {
		return propertyProjectionVisit(projection);
	}

	public String visit(final MinProjection projection) {
		return propertyProjectionVisit(projection);
	}

	public String visit(final SumProjection projection) {
		return propertyProjectionVisit(projection);
	}

	private String propertyProjectionVisit(final SinglePropertyProjection projection) {
		String hql = String.format("%s(%s)", propertyProjectionMapping.get(projection.getClass()), projection
				.getPropertyName());

		return hql;
	}

}
